package edu.cs.usask.ca.silver.plugin.rcp.dialogs.edit;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 
 * This class create a Add Input Files dialog box for the user to add Source
 * file and Input file on an existing Model Version without a specify source and
 * input file. After the source and input file are added, a default scenario is
 * then created "Original Scenario", under the current Model Version
 * 
 * @author Alan Yeung
 * 
 */
public class SetInputFileToModelVersionDialog extends Dialog {

	private Text sourceFileText;
	private Text jarFileText;

	private String sourceFilePath;
	private String jarFilePath;

	public SetInputFileToModelVersionDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Set Input Files");
	}

	/**
	 * a method to create elements and fields required to add source and input
	 * file for an existing model version without a source and input files
	 */

	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, false);
		composite.setLayout(layout);

		final Label sourceFileLabel = new Label(composite, SWT.END);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.END;
		sourceFileLabel.setLayoutData(gridData);
		sourceFileLabel.setText("&Source File:");

		sourceFileText = new Text(composite, SWT.BORDER);
		gridData = new GridData();
		gridData.widthHint = convertHeightInCharsToPixels(20);
		sourceFileText.setLayoutData(gridData);

		final Button getSourceFile = new Button(composite, SWT.PUSH | SWT.RIGHT
				| SWT.MouseUp);
		getSourceFile.setText("Browse");

		getSourceFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO multiple file and absolute path
				FileDialog file = new FileDialog(composite.getShell(), SWT.OPEN);
				file.setFilterExtensions(new String[] { "*.alp" });

				// default point to user.home
				file.setFilterPath(System.getProperty("user.home"));

				// Put in a readable name for the filter
				file.setFilterNames(new String[] { "AnyLogic alp files (*.alp)" });
				file.setText("Load Source File:");
				sourceFileText.setText(file.open());
			}
		});

		// Load Jarfile
		final Label jarFileLabel = new Label(composite, SWT.NONE);
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.END;
		jarFileLabel.setLayoutData(gridData);
		jarFileLabel.setText("&Input File:");
		jarFileLabel
				.setToolTipText("&Folder containing compiled class files in your "
						+ "AnyLogicWorkspace folder, usually named YOUR_SIMULATION_PACKAGE_BUILD/");

		jarFileText = new Text(composite, SWT.BORDER);
		gridData = new GridData();
		gridData.widthHint = convertHeightInCharsToPixels(20);
		jarFileText.setLayoutData(gridData);

		final Button getJarFile = new Button(composite, SWT.PUSH | SWT.RIGHT);
		getJarFile.setText("Browse");
		getJarFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO multiple file and absolute path
				DirectoryDialog file = new DirectoryDialog(composite.getShell()
						.getShell(), SWT.OPEN);

				// Constraint the path to AnyLogicWorkspace
				file.setFilterPath(System.getProperty("user.home")
						+ System.getProperty("file.separator")
						+ "AnyLogicWorkspace");

				// If not appeared, set AnyLogicWorkspacePro
				if (file.getFilterPath() == null) {
					file.setFilterPath(System.getProperty("user.home")
							+ System.getProperty("file.separator")
							+ "AnyLogicWorkspacePro");
				}

				// Set to user.home if none of AnyLogic standard or pro
				// workspace is presented.
				if (file.getFilterPath() == null) {
					file.setFilterPath(System.getProperty("user.home"));
				}

				file.setText("Choose the folder that contains compiled class files:");
				jarFileText.setText(file.open());

			}
		});
		return composite;
	}

	/**
	 * the data supplied is stored into local variable to be extracted using the
	 * get method by the AddModelVersionAction class. This method checks to make
	 * sure that all the fields are filled out
	 */

	@Override
	protected void okPressed() {

		sourceFilePath = sourceFileText.getText();
		jarFilePath = jarFileText.getText();
		if (sourceFilePath.equals("")) {
			MessageDialog.openError(getShell(),
					"Invalid Source File Selection",
					"Please choose the source file for this Model Version.");
			return;
		}
		if (jarFilePath.equals("")) {
			MessageDialog.openError(getShell(), "Invalid Jar File Selection",
					"Please choose the jar file for this Model Version.");
			return;
		}

		super.okPressed();
	}

	/**
	 * a method to get the new model version source file location
	 * 
	 * @return String containing the version source file location
	 */
	public String getSoureFilePath() {
		return sourceFilePath;
	}

	/**
	 * a method to get the new model version jar file location
	 * 
	 * @return String containing the version jar file location
	 * 
	 *         THIS SHOULD BE CHANGED TO DEAL WITH OTHER ASSOCIATED FILES
	 */
	public String getJarFilePath() {
		return jarFilePath;
	}

}
